extends CharacterBody2D

signal score

@export var speed = 10000

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.
@onready var dazhao: Timer = $Dazhao

@onready var PlayerCamera = $Perspective
@onready var joy_stick: TouchScreenButton = $Operate/CenterJoy/JoyStick
@onready var center_joy: Marker2D = $Operate/CenterJoy
@onready var center_a: Marker2D = $Operate/CenterA
@onready var center_b: Marker2D = $Operate/CenterB
@onready var center_da: Marker2D = $Operate/CenterDA

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	var val = Vector2.ZERO
	if Input.is_action_pressed("walk_up"):
		val.y-=1
	if Input.is_action_pressed("walk_down"):
		val.y+=1
	if Input.is_action_pressed("walk_left"):
		val.x-=1
	if Input.is_action_pressed("walk_right"):
		val.x+=1
	
	if Input.is_action_just_pressed("attack"):
		$Aker/Weapon/Animation.play("attack")
	if Input.is_action_just_pressed("attack1"):
		$Aker/Weapon/Animation.play("attack1")
	if Input.is_action_just_pressed("dazhao"):
		if dazhao.is_stopped():
			$Aker/Weapon/Animation.play("dazhao")
			dazhao.start()
	
	var temp = joy_stick.get_joystick_dir()
	if temp.length()>0:
		val = temp
	
	if val.length()>0:
		val.normalized()
		$AnimationPlayer.play("walk")
	else:
		$AnimationPlayer.pause()
	
	if val.x>0:
		$Aker.scale.x = 1
	elif val.x<0:
		$Aker.scale.x = -1
	
	velocity = val * delta * speed
	move_and_slide()

# 晃动相机---增强打击感
func _on_weapon_hited():
	var tween = PlayerCamera.create_tween()
	tween.tween_property(PlayerCamera,"offset",Vector2.ZERO,0.12).from(Vector2(15,-10))
	tween.parallel().tween_property(PlayerCamera,"zoom",Vector2(10,10),0.12).from(Vector2(9.9999,9.9999))
	score.emit()



func _on_dazhao_timeout() -> void:
	dazhao.stop()

func set_operate_position(joy_position,da_position,key_a_position,key_b_position):
	center_joy.position = joy_position
	center_da.position = da_position
	center_a.position = key_a_position
	center_b.position = key_b_position
